package com.tablefood.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.tablefood.dao.OrderDao;
import com.tablefood.domain.ShopOrder;
import com.tablefood.service.OrderService;
import com.tablefood.utils.PageOrderBean;

@Transactional
public class OrderServiceImpl implements OrderService {

	@Resource
	OrderDao orderDao;
	
	@Override
	public ShopOrder insertOrder(ShopOrder order) {
		System.out.println("生成订单");
		return orderDao.insertOrder(order);
	}
	
	@Override
	public ShopOrder updateOrder(ShopOrder order) {
		System.out.println("更新订单");
		return orderDao.updateOrder(order);
	}

	
	
	@Override
	public List searchOrder(String userid,String status) {
		System.out.println("查询订单");
		return orderDao.searchOrder(userid,status);
	}

	@Override
	public List searchOrderByTime(String orderid, String updatetime) {
		 
		return orderDao.searchOrderByTime(orderid, updatetime);
	}

	@Override
	public ShopOrder searchOrderByOrderId(Long orderid) {
		 
		return orderDao.searchOrderByOrderId(orderid);
	}

	@Override
	public boolean delByorderid(ShopOrder order) {
		 
		return orderDao.delByorderid(order);
	}

	@Override
	public PageOrderBean queryForPage(int pageSize, int page) {
		
		final String hql = "from ShopOrder u order by u.orderid desc"; // 查询语句
		int allRow = orderDao.getAllRowCount(hql); // 总记录数
		int totalPage = PageOrderBean.countTatalPage(pageSize, allRow); // 总页数
		final int offset = PageOrderBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageOrderBean.countCurrentPage(page); // 当前页
		List list = orderDao.queryForPage(hql, offset, length); //

		// 把分页信息保存到Bean当中
		PageOrderBean pageOrderBean = new PageOrderBean();
		pageOrderBean.setPageSize(pageSize);
		pageOrderBean.setCurrentPage(currentPage);
		pageOrderBean.setAllRow(allRow);
		pageOrderBean.setTotalPage(totalPage);
		pageOrderBean.setList(list);
		pageOrderBean.init();
		return pageOrderBean;
		 
	}

	@Override
	public List<ShopOrder> searchOrder(String userid) {
		System.out.println("查询所有订单");
		return orderDao.searchOrder(userid);
	}

	

	

	 
}
